﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.IO;

namespace BuisnessLayer
{
    public class ImageUtil
    {
        public static byte[] ImageToByteStream(string pImagePath) {
            byte[] image;
            using (var fs = new FileStream(pImagePath, FileMode.Open, FileAccess.Read))
            {
                using (var br = new BinaryReader(fs))
                {
                    image = br.ReadBytes((int)fs.Length);
                }
            }
            return image;
        }

        public static ImageSource BinaryToImageSource(byte[] pData) {
            var bi = new BitmapImage();
            bi.BeginInit();
            bi.StreamSource = new MemoryStream(pData);
            bi.EndInit();
            return bi;
        }

        //public static byte[] ImageSourceToBinary(ImageSource pData) {
        //    var imageSource = pData as BitmapImage; 
        //    byte[] data;
        //    JpegBitmapEncoder encoder = new JpegBitmapEncoder();
        //    encoder.Frames.Add(BitmapFrame.Create(imageSource));
        //    using (MemoryStream ms = new MemoryStream())
        //    {
        //        encoder.Save(ms);
        //        data = ms.ToArray();
        //    }
        //    return data;
        //}

        public static byte[] StringToByteArray(string str)
        {
            return Convert.FromBase64String(str);
        }

        public static string ByteArrayToString(byte[] input)
        {
            return Convert.ToBase64String(input);
        }
    }
}
